//
//  CarouselFigure.m
//  HappyBaby
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "FirstCell.h"
#import "UIImageView+WebCache.h"
#import "Define.h"
#import "UIImageView+WebCache.h"
#import "FirstModel.h"
@interface FirstCell ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollview;
@property(nonatomic,strong)UIPageControl *page;
@property(nonatomic,strong)UILabel *labelText;//显示标题
@property(nonatomic,strong)UIView *smallView;
@property(nonatomic,retain)UIImageView *myImageView;
@property(nonatomic,retain)NSMutableArray *curArray;//暂存当前页面显示的图片；
@property(nonatomic,strong)NSMutableArray *curTitleArray;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)FirstModel *model;

@end
@implementation FirstCell

{
    CGFloat width;
    CGFloat height;
    NSInteger curPage;//存放当前的page;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        CGFloat x = 0;
//        CGFloat y = 0;
         width = frame.size.width;
         height = 340 * width/640;
        //暂存当前页面显示的图片；
        self.curArray = [NSMutableArray arrayWithCapacity:3];
        self.curTitleArray = [NSMutableArray arrayWithCapacity:3];
//        self.titleArray = [NSMutableArray array];

        [self initScrollView];//初始化ScrollView;
        [self initSmallView];//初始化SmallView;
        [self initLabelText];//初始化labelText;
        [self initPageControl];//初始化page;
    }
    
    return self;
    
}

//初始化ScrollView
- (void)initScrollView
{
    CGFloat x = 0;
    CGFloat y = 0;

    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    self.scrollview.backgroundColor = [UIColor redColor];
    self.scrollview.contentSize = CGSizeMake(width*3, height);
    //一上来显示第二张图片
    self.scrollview.contentOffset = CGPointMake(width, 0);
    self.scrollview.delegate = self;
    self.scrollview.pagingEnabled = YES;//决定是否整屏翻动
    self.scrollview.showsHorizontalScrollIndicator = NO;
    self.scrollview.showsVerticalScrollIndicator = NO;
    [self creatImageView];//在scrollview里面添加imageView；
    
    
    [self.contentView addSubview:self.scrollview];
    
}
//初始化initSmallView;
- (void)initSmallView
{
    
    self.smallView = [[UIView alloc]initWithFrame:CGRectMake(0, height-30, width, 30)];
    self.smallView.backgroundColor = [UIColor blackColor];
    self.smallView.alpha = 0.4;
    [self.contentView addSubview:self.smallView];
    
}

//初始化labelText
- (void)initLabelText
{
    self.labelText = [[UILabel alloc]initWithFrame:CGRectMake(5, self.smallView.frame.origin.y, 130, 30)];
    //self.labelText.backgroundColor = [UIColor purpleColor];
    //self.labelText.text = @"精彩活动";
    self.labelText.font = [UIFont boldSystemFontOfSize:14];
    self.labelText.textColor = [UIColor whiteColor];
    [self.contentView addSubview:self.labelText];
    
}

//初始化pageControl
- (void)initPageControl
{
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(width-5-60, self.smallView.frame.origin.y, 30, 30)];
    [self.contentView addSubview:self.page];
    
}

//在scrollview里面创建3个视图到轮播图上面；
- (void)creatImageView
{
    for (int i = 0; i<3; i++)
    {
       UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(width*i, 0, width, height)];
        myImageView.userInteractionEnabled = YES;
        // 添加手势 添加一个tag
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [myImageView addGestureRecognizer:tap];
        myImageView.tag = 100 + i;
        
        [self.scrollview addSubview:myImageView];
        
    }
    
    
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView *imageView = (UIImageView *)tap.view;
    if (imageView.tag == 101)
    {
        [self.delegate skipWithModel:self.model];
    }
}

//更新当前视图对应的索引；
- (NSInteger)updatePage:(NSInteger)page
{
    NSInteger count = self.array.count;
    return (count + page) % count;
    
}

//更新当前对应的视图；
- (void)updateCurrentViewWithCurPage:(NSInteger)page
{
    NSInteger pre = [self updatePage:page - 1];
    curPage = [self updatePage:page];
    NSInteger last = [self updatePage:page + 1];
    [self.curArray removeAllObjects];
//    [self.curTitleArray removeAllObjects];
    
    [self.curArray addObject:self.array[pre]];
    [self.curArray addObject:self.array[curPage]];
    [self.curArray addObject:self.array[last]];
    
//    [self.curTitleArray addObject:self.titleArray[pre]];
//    [self.curTitleArray addObject:self.titleArray[curPage]];
//    [self.curTitleArray addObject:self.titleArray[last]];
    NSArray *subArray = self.scrollview.subviews;
    for (int i = 0; i<subArray.count; i++)
    {
        UIImageView *imageView = subArray[i];
        
        FirstModel *model = self.curArray[i];
        
        NSURL *url = [NSURL URLWithString:model.pic];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"2.png"]];
        model = self.curArray[1];
        self.model = model;
        self.labelText.text = model.title;
        /*
        NSURL *url = [NSURL URLWithString:self.curArray[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"2.png"]];
        self.labelText.text = self.curTitleArray[1];
         */
    }
    self.scrollview.contentOffset = CGPointMake(width, 0);
    self.page.currentPage = curPage;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    if (x >= width * 2)
    {
        [self updateCurrentViewWithCurPage:curPage + 1];
    }
    else if (x <= 0)
    {
        [self updateCurrentViewWithCurPage:curPage - 1];
        
    }
}

- (void)setArray:(NSArray *)array
{
    _array = array;
    self.page.numberOfPages = self.array.count;
    [self updateCurrentViewWithCurPage:0];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
}

- (void)autoPlay
{
    [self.scrollview setContentOffset:CGPointMake(width*2, 0) animated:YES];
    
}

//开始拖拽的时候停止定时器；
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    
}

//结束拖拽的时候开启定时器；
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
    //[self.timer fire];
   // [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}




@end
